package com.ajieee.merge.util;

import groovy.lang.GroovyShell;
import groovy.lang.Script;

import java.util.Map;

/**
 * @author ajieee
 * @date 2025-11-01
 */
public class GroovyUtils {

	private static final GroovyShell shell = new GroovyShell();

	public static Object execute(String scriptStr, Map<String, ?> context) {
		Script script = shell.parse(scriptStr);
		context.forEach(script::setProperty);

		return script.run();
	}

	public static void main(String[] args) {
		String s = """
				var rule1 = body <= total * 0.3
				var rule2 = lower >= (upper + body) * 2
				var rule3 = body >= upper * 2
				return rule1 && rule2 && rule3
				""";

		Script script = shell.parse(s);
		script.setProperty("total", 10);
		script.setProperty("body", 2);
		script.setProperty("upper", 1);
		script.setProperty("lower", 2);
		Object run = script.run();
		System.out.println("run = " + run);
	}

}
